;
(function() {
	var skill = {
		save: function() {
			db.update(config.dbname.coupon, this._id, this);
		},
		destroy: function() {
			db.remove(config.dbname.coupon, this._id);
		},
		setInfo: function(info) {
			//名称
			var name = String(info.name || "【优惠码】打折").trim();
			name = name.substr(0, 20); //限制不能超过20个字符
			if (!helper.folder_name(name)) {
				throwCusError("标题命名不规范")
			}
			this.info.name = name;
			//可用次数
			var times = +info.times || 1;
			this.info.times = times;
			//过期时间
			var diedline = +info.diedline || Number.MAX_VALUE;
			this.info.diedline = diedline;
			//类型
			var type = +info.type;
			if (!type && CouponCon.TYPE.hasOwnValue(type)) {
				throwCusError("找不到指定优惠码类型")
			}
			this.info.type = type;
			//类型对应的相关信息
			//打折：折扣比例，是否有相关梯度
			//满减：满多少，减多少，是否有相关梯度
			//抵用：可抵用的现金额度，是否有相关梯度
			var config = info.config || {};
			this.info.config = config;
			//描述
			var intro = String(info.intro || "");
			this.info.intro = intro || this.getIntro();
		},
		getIntro: function() {
			return CouponCon.getIntro(this);
		},
		toBuff: function() {
			var buff_id = this.buff_id;
			var buff;
			//无BUFF或者BUFF无效时，重新生成
			if (!buff_id || !(buff = BuffCon.get(buff_id))) {
				var buff = new BuffCon({
					name: this.info.name,
					code_id: "优惠码",
					config: {
						coupon_id: this._id
					}
				});
				buff_id = this.buff_id = buff._id;
				this.save();
			}
			return buff;
		}
	};
	module.exports = skill;

	var db = require("../db/index");
	var CouponCon = require("../coupon/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
}());